//
//  YYChildRecomedTableViewCell.swift
//  STMusic
//
//  Created by stoneobs on 17/4/6.
//  Copyright © 2017年 stoneobs. All rights reserved.
//
//**********************推荐cell***************************
import UIKit
import AVFoundation
let YYChildRecomedTableViewCellHeigth:CGFloat = 350 + 7.5;

class YYChildRecomedTableViewCell: UITableViewCell {

    var backGroundImageView:UIImageView?
    var iconButton:STButton?
    var nameButton:STButton?
    var titleLable:STLabel?
    var desButton:STButton?
    var musicDuringLabel:STLabel?//音频
    var musicListenLabel:STLabel?//收听
    var musicTimeLable:STLabel?//时间
    
    var avplayer:AVPlayer? //播放器
    
    var avplayerLayer:AVPlayerLayer?//播放界面
    
    var avplyerItem:AVPlayerItem? //播放源
    
    var imageClicHandel:() ->Void = {};
    
    
   public var model:YYChildRecomendModel! = YYChildRecomendModel(){
        didSet{
        
            self.backGroundImageView?.sd_setImage(with: URL.init(string: model.works.cover_photo), placeholderImage: nil, options:.retryFailed, completed: nil);
            
            self.iconButton?.sd_setBackgroundImage(with: URL.init(string: model.user_info.avatar!), for: .normal, placeholderImage: nil, options: .retryFailed );
            
            self.nameButton?.setTitle(model.user_info.nickname, for: .normal);
            
            self.titleLable?.text = model.works.title;
            
            self.desButton?.setTitle(model.works.tag_text, for: .normal);
            if model.works.tag_text == "" {
                self.desButton?.isHidden = true;
            }else{
                self.desButton?.isHidden = false;
            }
            
            
            var type:String
            if model.works.type == "1" {
                type = "音屏";
            }else{
                type = "视屏";
            }
            
            self.musicDuringLabel?.text = type + YYUtils.musicDuring(during: Int(model.works.file_long)!);
            
            self.musicListenLabel?.text = "收听" + String(model.works.play_num);
            
            self.musicTimeLable?.text = YYUtils.musicCreateTime(create: Int(model.create_time)!);
            
            //播放
           
        }
    }
    override  init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier);
        self.initSubViews();
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func initSubViews() -> Void {
        self.backgroundColor = YYThemeStyleManger.defultManger.gradualColor(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: YYChildRecomedTableViewCellHeigth));
        backGroundImageView = UIImageView.init(frame: .init(x: 7.5, y: 0, width: SCREEN_WIDTH - 15, height: 350));
        backGroundImageView?.image = UIImage.init(named: "2");
        backGroundImageView?.layer.cornerRadius = 4;
        backGroundImageView?.clipsToBounds = true;
        backGroundImageView?.contentMode = .scaleAspectFill;
        self.addSubview(backGroundImageView!);
        
        let grawView = UIView.init(frame: (backGroundImageView?.bounds)!);
        
        grawView.backgroundColor = UIColor.gray.withAlphaComponent(0.3);
        backGroundImageView?.addSubview(grawView);
        
        
        self.iconButton = STButton.init(frame: .init(x: 20, y: 20, width: 30, height: 30),
                                        title: "",
                                        titleColor: .white,
                                        titleFont: 15,
                                        cornerRadius: 15,
                                        backgroundColor: .clear,
                                        backgroundImage: UIImage.init(named: "3"),
                                        image: nil);
        self.iconButton?.setBrderWidth(1, borderColor: .black);
        backGroundImageView?.addSubview(self.iconButton!);
        self.iconButton?.showRoundShadow();
        
        self.nameButton = STButton.init(frame: .init(x: (self.iconButton?.right)! + 10, y: 25, width: 300, height: 20),
                                        title: "name",
                                        titleColor: .white,
                                        titleFont: 15,
                                        cornerRadius: 0,
                                        backgroundColor: .clear,
                                        backgroundImage: nil,
                                        image: nil);
        backGroundImageView?.addSubview(self.nameButton!);
        
        
        self.titleLable = STLabel.init(frame: .init(x: 25, y: 0, width: (backGroundImageView?.width)! - 30, height: 17),
                                       text: "title",
                                       textColor: .white,
                                       font: 14,
                                       isSizetoFit: false,
                                       textAlignment: .left)
        self.titleLable?.bottom = (backGroundImageView?.height)!  - 45;
        backGroundImageView?.addSubview(self.titleLable!);
        
        
        
        
        self.desButton = STButton.init(frame: .init(x: (iconButton?.left)!, y: (self.titleLable?.bottom)! + 3, width: (backGroundImageView?.width)! - 30, height: 16),
                                       title: "",
                                       titleColor: SecendTextColor,
                                       titleFont: 13,
                                       cornerRadius: 0,
                                       backgroundColor: .clear,
                                       backgroundImage: nil,
                                       image: UIImage.init(named: "ima_recomend_title"));
        self.desButton?.titleEdgeInsets = .init(top: 0, left: 5, bottom: 0, right: 0);
        backGroundImageView?.addSubview(self.desButton!);
        
        
        
        self.musicDuringLabel = STLabel.init(frame: .init(x: (iconButton?.left)!, y: (self.desButton?.bottom)! + 3, width: 60, height: 13),
                                             text: "4'43",
                                             textColor: .white,
                                             font: 10,
                                             isSizetoFit: false,
                                             textAlignment: .left)
        self.musicDuringLabel?.showRightLine(10);
        backGroundImageView?.addSubview(self.musicDuringLabel!);
        
        
        self.musicListenLabel = STLabel.init(frame: .init(x: (musicDuringLabel?.right)! + 10, y: (self.desButton?.bottom)! + 3, width: 60, height: 13),
                                             text: "1000",
                                             textColor: .white,
                                             font: 10,
                                             isSizetoFit: false,
                                             textAlignment: .left)
        backGroundImageView?.addSubview(self.musicListenLabel!);
        
        
        self.musicTimeLable = STLabel.init(frame: .init(x: (backGroundImageView?.width)! - 20 - 60 , y: (self.desButton?.bottom)! + 3, width: 60, height: 13),
                                             text: "17",
                                             textColor: .white,
                                             font: 10,
                                             isSizetoFit: false,
                                             textAlignment: .right)
        backGroundImageView?.addSubview(self.musicTimeLable!);
        
        
        //avplayerlayer - 固定
//        self.avplyerItem = AVPlayerItem.init(url: URL.init(string: "https://video-cdn.suiyueyule.com/o_1bdc2n9291tmcs4e1oj11k448gc.ts.mp4?auth_key=1491914357-0-0-0f460d34345c2cdfb3d85d43994ffac1")!);
//        self.avplayer = AVPlayer.init(playerItem: self.avplyerItem);
//        self.avplayerLayer = AVPlayerLayer.init(player: self.avplayer);
//        self.avplayerLayer?.frame = CGRect.init(x: 0, y: 0, width: (backGroundImageView?.width)!, height: (backGroundImageView?.height)!);
//        self.backGroundImageView?.layer.addSublayer(self.avplayerLayer!);
//        self.avplayer?.play();
        backGroundImageView?.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(imageViewAction)))
        backGroundImageView?.isUserInteractionEnabled = true;
        
    }
    func imageViewAction() -> Void {
        self.imageClicHandel();
    }
}
